<?php
/**
 * Контроллер управления дополнениями
 */
class AddonsController extends Zend_Controller_Action {

    public function preDispatch() {
        $session = new Zend_Session_Namespace('UnCMS');
        if (!isset($session->acl->addons))
            $this->_redirect(SITE_URL);
        $t = Zend_Registry::get('Zend_Locale');
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        }
        $this->view->navigation_line = array($t->_('Addons') => ADMIN_URL . '/addons');
    }

    public function indexAction() {
        $model = new AddonsModel();
        $forms = new AddonsForm();
        $form = $forms->upload();
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploadedData = $form->getValues();
                $file = $form->addon->getFileName();
                $id = $model->installAddon($file);
            }
        }
        $this->view->form = $form;
        $this->view->modules = $model->getAddons('page');
        $this->view->templates = $model->getAddons('template');
        $this->view->languages = $model->getAddons('language');
    }

    public function deleteAction() {
        $id = (int) $this->getRequest()->getParam('id', 0);
        $model = new AddonsModel();
        if ($id > 0)
            $model->deleteAddon($id);
        $this->_redirect(ADMIN_URL . '/addons');
    }

}
